/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.components.documents;

/**
 *
 * @author glauco
 */
public class DateTimeFilter implements DocumentFilter{

    public static final String barra = "/";
    public static final String ponto = ";";
    public static final String espaco = " ";

    @Override
    public String beforeInsert(String actualValue, int offSet, String adding) {

        int barras = 0;
        int pontos = 0;
        int espacos = 0;

        if (actualValue.contains(barra))
            barras = actualValue.split(barra).length - 1;

        if (actualValue.contains(ponto))
            pontos = actualValue.split(ponto).length - 1;

        if (actualValue.contains(espaco))
            espacos = actualValue.split(espaco).length - 1;

        // permite apenas digitos
        for (char c : adding.toCharArray()) {

            // Se for um digito, passou.
            if(Character.isDigit(c))
                continue;

            if(c == '/'){
                if(barras == 2)
                    return null;
                barras++;
                continue;
            }

            if(c == ':'){
                if(pontos == 2)
                    return null;
                pontos++;
                continue;
            }

            if(c == ' '){
                if(espacos == 1)
                    return null;
                espacos++;
                continue;
            }

            return null;
        }

        return adding;
    }

}
